/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kleinberg;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author veena
 */
public class ScaleOutput {
    String dfile="/home/veena/veena/f14/660/TG/obamacare/mitt2012/file3-out";
    String outf="/home/veena/Desktop/outf.txt";
    //scale down output
    //make excel sheet to draw output
    //convert seconds-miliseconds-dates
    
    
    public void scaleOuts(){
        double st=0,end=0;
        String[] sl=null;
        ArrayList<String> outs=new ArrayList<String>();
//        ArrayList<String> reduceDimension_offset=new ArrayList<String>();
//        ArrayList<String> reduceDimension_output=new ArrayList<String>();
//        ArrayList<String> convertedDates=new ArrayList<String>();
//        ArrayList<String> combined=new ArrayList<String>();
        int levels=0;
//        SingleStream sst=new SingleStream();
        ScaleOutput soutp=new ScaleOutput();
        
//        //get offsets
//        offsets=sst.readminutes();
        
        
        //get outputs
        outs=soutp.readOutputpts();
        //get #of levels
        levels=soutp.numoflevels(outs);
        //convert it to dates
//        convertedDates=soutp.convertodate(outs);
        //reduce dimension offset
//        reduceDimension_offset=soutp.reducedimension_offset(offsets);
//        
////        //reduce dimensions-divide by 10000000
//        reduceDimension_output=soutp.reducedimension_output(outs);
        //get start and end of series for plot
        sl=outs.get(0).split("-");
        st=Double.valueOf(sl[1].trim())/1000;///1000000
        end=Double.valueOf(sl[2].trim())/1000;///1000000
//        combined=soutp.combined(outs);
        //print out csv sheets 
        soutp.printvalues(levels,outs,st,end);
        outs.size();
    }
    
    private void printvalues(int numlevel,ArrayList out,double st,double end){
        ScaleOutput soutp=new ScaleOutput();
        double level=0.0;
        String range="";
        String[] split=null;
        ArrayList<String> rangevals=new ArrayList<String>();
//        ArrayList<String> temp=new ArrayList<String>();
        String[] spl1=null,spl2=null;
        for(double i=0;i<numlevel;i++){
            level=i+1.0;
            rangevals=new ArrayList<String>();
            for(int j=0;j<out.size();j++){
                
                //get all range of values for this level
                split=out.get(j).toString().split("-");
                range=split[0].replace("\"", "").trim();
                if(range.equals(String.valueOf(level))){
                rangevals.add((String) out.get(j));
//                System.out.println(out.get(j));
                }
                
                
            }//saved all ranges for this 
            System.out.println("level"+level);
            soutp.trialprint(rangevals,level,st,end);
            //print values based on this collected range
        }
            int j=0,k=0,i=0;
            
        
    }
    //print values for a particular level
    private void trialprint(ArrayList range,double level,double st,double end){
      BufferedWriter buff=null;
        try {
            //        boolean check=false;
            
            ScaleOutput soutp=new ScaleOutput();
            buff=new BufferedWriter(new FileWriter(new File(outf)));
            double tmp=0;
            DecimalFormat fourDForm =new DecimalFormat("#");
            tmp = Double.valueOf(fourDForm.format(tmp));
            String[] spl=null;
            double val=0,b=0,l=0;
            val=st;
            val=Math.floor(val);
            tmp=Double.valueOf(fourDForm.format(tmp));
            for(int i=0;i<range.size();i++){
                spl=range.get(i).toString().replace("\"", "").split("-");
                b=Double.valueOf(spl[1].trim())/1000;///1000000
                l=Double.valueOf(spl[2].trim())/1000;///
                
                //keep
                while(val<Math.ceil(b) & val<Math.ceil(l) & val<=end){
                    val++;
//             tmp++;
//             System.out.println("-");
                    buff.write("-");
            buff.newLine();
                }
                
                while(val>=Double.valueOf((b))& val<=Double.valueOf((l)) & val<=end){
//             tmp=val;
                    val++;
//             tmp++;
//             System.out.println(level);//+tmp
                    buff.write(String.valueOf(level));
            buff.newLine();
                }
                if(val>l & val<=end){
                    //goto next record
                    
                }
            }  } catch (IOException ex) {
            Logger.getLogger(ScaleOutput.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                buff.close();
            } catch (IOException ex) {
                Logger.getLogger(ScaleOutput.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
//    
//    private void write(String val, BufferedWriter buff){
//        
//        try {
//            
//            
//            
//        } catch (IOException ex) {
//            Logger.getLogger(ScaleOutput.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    private int numoflevels(ArrayList out){
        String[] spl=null;
        ArrayList<String> levels=new ArrayList<String>();
        String str="";
        for(int i=0;i<out.size();i++){
            str=(String) out.get(i);
            spl=str.split("-");
            if(!levels.contains(spl[0]))
                levels.add(spl[0].trim());
            
//                    return 0;
        }
        return levels.size();
    }
    private ArrayList combined(ArrayList outs){
        long millis=0;
        double val=0.0;
        ArrayList<String> temp=new ArrayList<String>();
        Calendar cal=Calendar.getInstance();
        Date d1=null,d2=null;
        SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss +SSSS yyyy");
        String stream="",tmp;
        long s1=0,s2=0;
                String[] spl=null;
        for (int k=0;k<outs.size();k++) {
            tmp="";stream="";
            stream=(String) outs.get(k);
            tmp=stream+";";
            //get dates and append it
            spl = stream.split("-");
            stream="";
            stream=spl[0]+"-";
//            tmp+=stream;
            s1=Long.valueOf(spl[1].trim());
            s2=Long.valueOf(spl[2].trim());
            //st date
            s1=s1*1000;
            cal.setTimeInMillis(s1);
            d1=cal.getTime();
            stream+=sdf.format(d1)+"-";
            //end date
            s2=s2*1000;
            cal.setTimeInMillis(s2);
            d2=cal.getTime();
            stream+=sdf.format(d2);
//            tmp+=stream+";";
            //get reduced dimension and append it here
            stream+=";"+spl[0]+"-";
            for(int i=1;i<spl.length;i++){
                val=Double.valueOf(spl[i]);
                val=val/10000;
                if(i==spl.length-1)
                    stream+=val;
                else
                    stream+=val+"-";
            }
            tmp+=stream;
            temp.add(tmp);
        }
        
        return temp;
        
    }
    private ArrayList convertodate(ArrayList outs){
        long millis=0;
        Calendar cal=Calendar.getInstance();
        Date d1=null,d2=null;
        SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss +SSSS yyyy");
        String stream="";
        long s1=0,s2=0;
                String[] spl=null;
                
    for (int k=0;k<outs.size();k++) {
            stream=(String) outs.get(k);
            spl = stream.split("-");
            stream="";
            stream=spl[0]+"-";
            s1=Long.valueOf(spl[1].trim());
            s2=Long.valueOf(spl[2].trim());
            //st date
            s1=s1*1000;
            cal.setTimeInMillis(s1);
            d1=cal.getTime();
            stream+=sdf.format(d1)+":";
            //end date
            s2=s2*1000;
            cal.setTimeInMillis(s2);
            d2=cal.getTime();
            stream+=sdf.format(d2);
            
            outs.set(k, stream);
        }
    return outs;
        
        
    }
    private ArrayList reducedimension_offset(int[] outs){
        double val=0.0;
        int stream=0;
        ArrayList<String> str=new ArrayList<String>();
                String[] spl=null;

    for (int k=0;k<outs.length;k++) {
            stream= outs[k];
                            val=Double.valueOf(stream);
                val=val/10000;
               str.add(String.valueOf(val));
        }
    return str;
    }
    private ArrayList reducedimension_output(ArrayList outs){
        double val=0.0;
        String stream="";
                String[] spl=null;

    for (int k=0;k<outs.size();k++) {
            stream=(String) outs.get(k);
            spl = stream.split("-");
            stream="";
            stream=spl[0]+"-";
            for(int i=1;i<spl.length;i++){
                val=Double.valueOf(spl[i]);
                val=val/10000;
                stream+=val+"-";
            }
            outs.set(k, stream);
        }
    return outs;
    }
    private ArrayList readOutputpts(){
        ArrayList<String> vals=new ArrayList<String>();
         BufferedReader buff=null;
        try {
           
            buff = new BufferedReader(new FileReader((dfile)));
            String s=null;String[] spl=null;
            buff.readLine();
            while((s=buff.readLine())!=null){
                vals.add(s);
                
            }
            buff.close();
            return vals;
        } catch (IOException ex) {
            Logger.getLogger(ScaleOutput.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
             try {
                 buff.close();
             } catch (IOException ex) {
                 Logger.getLogger(ScaleOutput.class.getName()).log(Level.SEVERE, null, ex);
             }
            }
        return null;
    }
}
